package com.spring.oaassist.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spera.shopoa.util.ShopOaSessionUtil.OaUser;
import com.spring.oaassist.web.form.CardOperatorLog;
import com.spring.oaassist.web.service.CardOperatorService;
import com.spring.security.core.annotation.OaUserPermission;
import com.spring.tools.Result;
import com.spring.tools.ValidatorResultHandler;

/**
 * 卡操作
 * @author wojunyu
 *
 */
@Controller
@RequestMapping(value = "/cardOPerator")
public class CardOperatorController {
	
	@Resource private CardOperatorService cardOperatorService;
	
	/**
	 * 卡操作页面
	 * @param request
	 * @return
	 */
	@OaUserPermission(value="/cardOPerator/index")
	@RequestMapping(value = "/index")
    public ModelAndView index(HttpServletRequest request){
		return new ModelAndView("main/cardoperator/index");
    }
	
	@RequestMapping(value = "/cardOperatorLog")
    public ModelAndView cardOperatorLog(String id,HttpServletRequest request){
		List<Map<String, Object>> m = cardOperatorService.queryCardOperatorLog(id);
		Map<String,Object> retM = Maps.newHashMap();
		retM.put("logList", m); 
		return new ModelAndView("main/cardoperator/cardoperatorlog",retM);
    }
	
	
	/**
	 * 查询卡
	 * @param page
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/queryCardOperator")
    @ResponseBody
    public Object queryCardOperator(String phone,String kids_parkId,String member_isvaild,String page,String pageSize,HttpServletRequest request){
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
        Map<String, Object> parm = new HashMap<String, Object>();
        Map<String, Object> retD = new HashMap<String, Object>();
        if(StringUtils.isBlank(kids_parkId)){
        	kids_parkId = oaUser.getKids_park_id();
        }
        parm.put("phone", phone);
        parm.put("kids_parkId", kids_parkId);
        parm.put("member_isvaild", member_isvaild);
    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));
        List<Map<String,Object>> list = cardOperatorService.queryCardOperator(parm);
        PageInfo pageObj = new PageInfo(list);
        retD.put("total", pageObj.getTotal());
        retD.put("data", list);
        return retD;
    }
	
	/**
	 * 保存
	 * @param task
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveCardOperator")
    @ResponseBody
    public Object saveCardOperator(@RequestBody @Valid CardOperatorLog cardOperatorLog,BindingResult result,HttpServletRequest request){
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		Result retVal = ValidatorResultHandler.handle(result);
		if("0".equalsIgnoreCase(retVal.getStatus())){
			return retVal;
		}
		if("1".equalsIgnoreCase(cardOperatorLog.getOperator_isvalid())){
			if(StringUtils.isBlank(cardOperatorLog.getNotes())){
				retVal.setStatus("0");
				retVal.setMsg("注销/挂失操作备注不可为空");
				return retVal;
			}
		}
		cardOperatorLog.setOperator_id(oaUser.getOa_user_id());
		cardOperatorLog.setOperator_name(oaUser.getOa_user_name());
		try{
			retVal = cardOperatorService.saveCardOperator(cardOperatorLog);
		}catch(Exception e){
			e.printStackTrace();
			retVal.setStatus("0");
			retVal.setMsg("操作异常");
		}
        return retVal;
    }
	
	
	
	@RequestMapping(value = "/getKidsList")
    @ResponseBody
    public Object getKidsList(HttpServletRequest request){
        return cardOperatorService.getKidsList();
    }
	
}
